package com.jcl.core.resp;

import java.util.Date;

public final class ApiResponse<T> {

    public static ApiResponse _response = new ApiResponse();

    public static final String TYPE_SUCCESS           = "success";
    public static final String TYPE_ERROR             = "error";
    public static final String TYPE_WARNING           = "warning";
    public static final String TYPE_FAILURE           = "failure";
    public static final String CODE_SUCCESS           = "200";
    public static final String CODE_ERROR_DEFAULT     = "50001";
    public static final String CODE_WARNING_DEFAULT   = "60001";

    public static <T> ApiResponse success(String msg){
        ApiResponse response = newInstance();
        return contract(response, TYPE_SUCCESS, CODE_SUCCESS, msg);
    }
    public static <T> ApiResponse success(String msg, T Data){
        ApiResponse response = newInstance(Data);
        return contract(response, TYPE_SUCCESS, CODE_SUCCESS, msg);
    }
    public static <T> ApiResponse success(String msg, String code, T Data){
        ApiResponse response = newInstance(Data);
        return contract(response, TYPE_SUCCESS, code, msg);
    }
    public static <T> ApiResponse success(T Data){
        ApiResponse response = newInstance(Data);
        return contract(response, TYPE_SUCCESS, CODE_SUCCESS, "OK");
    }
    public static <T> ApiResponse error(String msg){
        ApiResponse response = newInstance();
        return contract(response, TYPE_ERROR, CODE_ERROR_DEFAULT, msg);
    }
    public static <T> ApiResponse error(String msg, String code){
        ApiResponse response = newInstance();
        return contract(response, TYPE_ERROR, code, msg);
    }
    public static <T> ApiResponse error(String msg, String code, T Data){
        ApiResponse response = newInstance(Data);
        return contract(response, TYPE_ERROR, code, msg);
    }

    public static <T> ApiResponse warning(String msg, String code, T Data){
        ApiResponse response = newInstance(Data);
        return contract(response, TYPE_WARNING, code, msg);
    }

    public static <T> ApiResponse warning(String msg, String code){
        ApiResponse response = newInstance();
        return contract(response, TYPE_WARNING, code, msg);
    }

    public static <T> ApiResponse warning(String msg){
        ApiResponse response = newInstance();
        return contract(response, TYPE_WARNING, CODE_WARNING_DEFAULT, msg);
    }

    private static ApiResponse contract(ApiResponse response, String type, String code, String msg){
        response.setType(type);
        response.setMsg(msg);
        response.setCode(code);
        return response;
    }

    public static <T> ApiResponse newInstance(T t){
        ApiResponse response = new ApiResponse();
        response.setData(t);
        return response;
    }

    public static ApiResponse newInstance(){
        return _response;
    }

    private String code;
    private String msg;
    private String type;
    private T data;
    private static Date timestamp = new Date();

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static Date getTimestamp() {
        return timestamp;
    }

    public static void setTimestamp(Date timestamp) {
        ApiResponse.timestamp = timestamp;
    }

    public ApiResponse(){
        this.type = "";
        this.code = "200";
        this.msg = "操作成功";
    }
}